package com.medical.medical.service;

import com.medical.medical.entity.Hospital;
import com.medical.medical.mapper.HospitalMapper;
import com.medical.medical.util.Constants;
import com.medical.medical.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HospitalServiceImpl
 * @Description 医院-接口实现类
 * @Author WLD
 * @Date 2022/9/14 8:57
 * @Version 1.0
 */
@Service
@Slf4j
public class HospitalServiceImpl implements HospitalService{


    @Resource
    private HospitalMapper hospitalMapper;


    /**
      * @Description 获得医院信息实现方法
      * @Author zxs
      * @Date 2022/9/14 16:52
      * @Params inInfo输入信息
      * @Returns hospitalInfoList
      */
    @Override
    public List<Hospital> getHospitalInfo(String inInfo) {

        List<Hospital> hospitalList = hospitalMapper.getHospitalInfo(inInfo);
        return hospitalList;
    }

    @Override
     /**
      *@author WangLuDong
      *@Description 方法描述
      *@Date 2022-09-21 14:12:32
       * @param hName
      *@Return {@link int}
      */
    public int getHospitalCount(String hName) {
        int hospitalCount = 0;
        try {
            hospitalCount = hospitalMapper.getHospitalCount(hName);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取医院总数量时发生异常！");
        }
        return hospitalCount;
    }

    @Override
     /**
      *@author WangLuDong
      *@Description 方法描述 根据医院名模糊查询医院，并分页
      *@Date 2022-09-21 14:35:13
       * @param hName
      * @param current
      *@Return {@link Map< String, Object>}
      */
    public Map<String, Object> getHospitalByHName(String hName, int current) {
        Map<String, Object> resultMap = ResultUtil.success("医院信息获取成功");
        /**
         * limit N,M : 从第 N 条记录开始【不包括N】, 返回 M 条记录
         */
        int  start = (current - 1) * Constants.PAGE_SIZE;
        try {
            List<Hospital> hospitalList = hospitalMapper.getAllHospital(hName,start,Constants.PAGE_SIZE);
            log.info("{}",hospitalList);
            if (hospitalList != null && !hospitalList.isEmpty()){
                resultMap.put("hospitalList",hospitalList);
            }else {
                resultMap = ResultUtil.successNull("查询不到医院信息");
            }
            log.info("查询结束：{}",resultMap.get("message"));
            return resultMap;
        }catch (Exception e) {
            e.printStackTrace();
            log.error("程序异常");
        }
        return ResultUtil.error("医院信息获取失败");
    }

    /**
     *@author WangLuDong
     *@Description 方法描述 添加医院信息
     *@Date 2022-09-21 15:10:35
     * @param hospital
     *@Return {@link boolean}
     */
    @Override
    public boolean addHospital(Hospital hospital) {
        try {
            return hospitalMapper.addHospital(hospital) > 0;
        }catch (Exception e) {
            e.printStackTrace();
            log.error("添加医院信息失败");
        }
        return false;
    }

    /**
     *@author WangLuDong
     *@Description 方法描述 根据医院id修改医院信息
     *@Date 2022-09-21 15:27:07
     * @param hospital
     *@Return {@link boolean}
     */
    @Override
    public boolean updateHospitalByHId(Hospital hospital) {
        try {
            return hospitalMapper.updateHospitalByHId(hospital) > 0;
        }catch (Exception e) {
            e.printStackTrace();
            log.error("修改医院信息失败");
        }
        return false;
    }

    /**
     *@author WangLuDong
     *@Description 方法描述 根据医院id,删除指定医院信息
     *@Date 2022-09-21 15:43:10
     * @param hId
     *@Return {@link boolean}
     */
    @Override
    public boolean deleteHospitalByHId(int hId) {
        try {
            return hospitalMapper.deleteHospitalByHId(hId) > 0;
        }catch (Exception e) {
            e.printStackTrace();
            log.error("修改医院信息失败");
        }
        return false;
    }
    /**
      * @description 根据医院id获得医院详细信息
      * @author GUOYN
      * @date 2022-09-24 21:48:34  * @param hId
      * @return {@link Hospital}
      */
    @Override
    public Map<String, Object> getHospitalViewById(int hId) {
        Map<String, Object> resultMap = ResultUtil.success("医院详细信息获取成功");
        try {
            Hospital hospital = hospitalMapper.getHospitalViewById(hId);
            log.info("{}",hospital);
            if (hospital != null){
                resultMap.put("hospital",hospital);
            }else {
                resultMap = ResultUtil.successNull("查询不到医院详细信息");
            }
            log.info("查询结束：{}",resultMap.get("message"));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("程序异常");
        }
        return ResultUtil.error("医院详细信息获取失败");
    }
}
